<include file="public@header"/>
</head>
<body>
<div class="wrap js-check-wrap">
    <ul class="nav nav-tabs">
        <empty name="adding">
            <li class="active"><a href="{:url('AdminOrder/add')}">订单草稿</a></li>
                <li><a href="{:url('AdminOrder/add')}?force=1">创建订单</a></li>
            <else/>
            <li><a href="{:url('AdminOrder/add')}">订单草稿</a></li>
            <li class="active"><a>创建订单</a></li>
        </empty>
    </ul>
    <if condition="$adding">
        <form class="margin-top-20 js-ajax-form" method="post" action="{:url('AdminOrder/addPost')}">
            <div class="well form-inline ">
                <div class="row">
                    <div class="col-md-6">
                        <span class="form-required">*</span>选择客户:
                        <input type="text" class="form-control" name="name"
                               value="{$order_draft.customer.name|default=''}"
                               onclick="doSelectCustomer();"
                               id="js-customer-name-input">
                    </div>
                    <notempty name="order_draft.customer.name">
                        <div class="col-md-6">
                            手机：
                            <input type="text" class="form-control" value="{$order_draft.customer.mobile|default=''}"
                                   readonly>
                        </div>
                    </notempty>
                </div>
                <notempty name="order_draft.customer.name">
                    <br>
                    <div class="row">
                        <div class="col-md-6">
                            电子邮箱：
                            <input type="text" class="form-control" value="{$order_draft.customer.email|default=''}"
                                   readonly/></div>
                        <div class="col-md-6">
                            单位：
                            <input type="text" class="form-control" value="{$order_draft.customer.company|default=''}"
                                   readonly/></div>
                    </div>
                    <br>
                </notempty>
                <notempty name="order_draft.customer.name">
                    <div class="row">
                        <div class="col-md-4">
                            联系地址：
                            <input type="text" class="form-control" value="{$order_draft.customer.address|default=''}"
                                   readonly/>
                        </div>
                    </div>
                    <br>
                </notempty>
                <input type="hidden" name="customer_id" id="js-customer-id-input"/>
                <input type="hidden" name="user_id" value="{$order_draft.user_id|default=''}"/>
                <input type="hidden" name="id" value="{$order_draft_id}"/>
            </div>
            <div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
                <div class="panel panel-default">
                    <div class="panel-heading" role="tab" id="headingOne">
                        <h4 class="panel-title">
                            <a role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseOne"
                               aria-expanded="true" aria-controls="collapseOne">
                                选择产品
                            </a>
                        </h4>
                    </div>
                    <div id="collapseOne" class="panel-collapse collapse in" role="tabpanel"
                         aria-labelledby="headingOne">
                        <table class="table table-bordered">
                            <tr>
                                <th>商品</th>
                                <td>包装规格</td>
                                <th>价格</th>
                                <th>数量</th>
                                <th>金额</th>
                                <th>操作</th>
                            </tr>
                            <notempty name="order_draft.items">
                                <foreach name="order_draft.items" item="vo">
                                    <tr>
                                        <td>{$vo.goods_name}</td>
                                        <td>{$vo.goods_spec}</td>
                                        <td>{$vo.goods_price}</td>
                                        <td>
                                            <input value="{$vo.goods_quantity}" type="number"
                                                   class="form-control input-sm input-goods-quantity"
                                                   style="width:60px;"
                                                   data-key="{$key}"
                                                   min="1">
                                        </td>
                                        <td>
                                            <span class="input-goods-amount-{$key}">
                                                {:number_format(round($vo.goods_price * $vo.goods_quantity,2),2)}
                                            </span>
                                        </td>
                                        <td>
                                            <a href="{:url('AdminOrderDraft/deleteItem',array('id'=>$order_draft_id,'key'=>$key))}"
                                               class="js-ajax-delete">
                                                {:lang('DELETE')}
                                            </a>
                                        </td>
                                    </tr>
                                </foreach>
                            </notempty>
                            <tfoot>
                            <tr>
                                <td colspan="6" class="text-center">
                                    <a href="javascript:doSelectProducts()">+ 选择产品</a>
                                </td>
                            </tr>
                            </tfoot>
                        </table>
                    </div>
                </div>
            </div>
            <div class="text-center">
                <button type="submit" class="btn btn-primary js-ajax-submit" id="js-submit">保存</button>
                <button type="submit" class="btn btn-success js-ajax-submit" data-action="{:url('AdminOrder/confirm')}"
                        title="请一定保存后，再进行此操作！" data-toggle="tooltip">完成并生成订单
                </button>
            </div>
        </form>
        <else/>
        <table class="table table-hover table-bordered table-list margin-top-20">
            <thead>
            <tr>
                <th align="center">ID</th>
                <th>用户</th>
                <th>创建时间</th>
                <th width="120">{:lang('ACTIONS')}</th>
            </tr>
            </thead>
            <tbody>
            <foreach name="order_drafts" item="vo">
                <php>$customer=json_decode($vo['customer'],true);</php>
                <tr>
                    <td>{$vo.id}</td>
                    <td>
                        <notempty name="customer.name">
                            <div>
                                姓名：{$customer.name|default=''}<br>
                                手机：{$customer.mobile|default=''}<br>
                                邮箱：{$customer.email|default=''}<br>
                                客户职务：{$customer.job_position|default=''}<br>
                                联系地址：{$customer.address|default=''}<br>
                            </div>
                            <else/>
                            未选择用户
                        </notempty>

                    </td>
                    <td>{:date('Y-m-d H:i',$vo['create_time'])}</td>
                    <td>
                        <a href="{:url('AdminOrder/add',array('id'=>$vo['id']))}">继续操作</a>
                        <a href="{:url('AdminOrder/deleteDraft',array('id'=>$vo['id']))}" class="js-ajax-delete">删除</a>
                    </td>
                </tr>
            </foreach>
            </tbody>
        </table>
    </if>
</div>
<script src="__STATIC__/js/admin.js"></script>
<script>
    function doSelectCustomer() {
        var selectedCategoriesId = $('#js-categories-id-input').val();
        parent.openIframeLayer("{:url('crm/AdminCustomer/select')}?ids=" + selectedCategoriesId, '请选择客户', {
            area: ['95%', '90%'],
            btn: ['确定', '取消'],
            yes: function (index, layero) {
                //do something

                var iframeWin        = parent.window[layero.find('iframe')[0]['name']];
                var selectedCustomer = iframeWin.confirm();
                if (!selectedCustomer.id) {
                    parent.layer.msg('请选择客户');
                    return;
                }
                $('#js-customer-id-input').val(selectedCustomer.id);
                $('#js-customer-name-input').val(selectedCustomer.name);
                $('#js-submit').click();
                //console.log(layer.getFrameIndex(index));
                parent.layer.close(index); //如果设定了yes回调，需进行手工关闭
            }
        });
    }

</script>
<if condition="$adding">
    <script>
        function doSelectProducts() {
            parent.openIframeLayer("{:url('chem/AdminProduct/select')}?order_draft_id={$order_draft_id}", '请添加产品', {
                area: ['95%', '90%'],
                btn: ['确定', '取消'],
                yes: function (index, layero) {
                    var iframeWin = parent.window[layero.find('iframe')[0]['name']];
                    parent.layer.close(index); //如果设定了yes回调，需进行手工关闭
                },
                end: function () {
                    window.location.href = "{:url('AdminOrder/add')}?id={$order_draft_id}";
                }
            });
        }

        $(function () {
            $('.input-goods-quantity').on('change', function () {
                var $this    = $(this);
                var key      = $this.data('key');
                var quantity = $this.val();
                if ($this.data('loading')) {
                    return;
                }
                $this.data('loading', true);

                $.ajax({
                    url: "{:url('order/AdminOrderDraft/changeItemQuantity')}",
                    type: 'POST',
                    dataType: 'JSON',
                    data: {id: '{$order_draft_id}', quantity: quantity, key: key},
                    success: function (data) {
                        if (data.code == 1) {
                            $('.input-goods-amount-' + key).text(data.data.goods_amount);
                            //$('.total-amount-text').text(data.data.total_amount);
                        }

                        if (data.code == 0) {
                            if ($this.is(':checked')) {
                                $this.attr('checked', false);
                            } else {
                                $this.attr('checked', 'checked');
                            }
                        }
                    },
                    error: function () {
                    },
                    complete: function () {
                        $this.data('loading', false);
                    }
                });

            });
        });
    </script>
</if>
</body>
</html>